package miscellaneous;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * This class helps different situations connecting
 * with a calendar and how to format the visual
 * @author daniel
 */
public class TimeSupport {
	
	public static String[] daysByNumber = {"Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag", "Lørdag", "Søndag"};

	/**
	 * Converts a String date to a Calendar object
	 * @param textDate on format HH:mm:ss dd.MM.yyyy
	 * @return Calendar object based on the String date
	 */
	public static Calendar convertStringDateToCalendar(String textDate) {
		Calendar calendar = new GregorianCalendar(Locale.FRANCE);
		try {
			int year = Integer.parseInt(textDate.substring(15,19));
			int month = Integer.parseInt(textDate.substring(12,14));
			int day = Integer.parseInt(textDate.substring(9,11));
			int hour = Integer.parseInt(textDate.substring(0,2));
			int minute = Integer.parseInt(textDate.substring(3,5));
			int second = Integer.parseInt(textDate.substring(6,8));
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, month-1);
			calendar.set(Calendar.DAY_OF_MONTH, day);
			calendar.set(Calendar.HOUR_OF_DAY, hour);
			calendar.set(Calendar.MINUTE, minute);
			calendar.set(Calendar.SECOND, second);
			return calendar;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * @param calendar
	 * @return a nice formatted String containing only the date
	 */
	public static String showDateNice(Calendar calendar) {
		DecimalFormat d = new DecimalFormat("00");
		return d.format(calendar.get(Calendar.DAY_OF_MONTH)) + "." +
				d.format(calendar.get(Calendar.MONTH)+1) + "." +
				calendar.get(Calendar.YEAR);
	}
	
	/**
	 * 
	 * @param calendar
	 * @return a nice formatted String containing only the time
	 */
	public static String showTimeNice(Calendar calendar) {
		DecimalFormat d = new DecimalFormat("00");
		return d.format(calendar.get(Calendar.HOUR_OF_DAY)) + ":" +
				d.format(calendar.get(Calendar.MINUTE));
	}
}